IF OBJECT_ID('fGetFragment') IS NOT NULL 
BEGIN 
    DROP FUNCTION fGetFragment
    IF OBJECT_ID('fGetFragment') IS NOT NULL
        PRINT '<<< FAILED DROPPING FUNCTION fGetFragment >>>'
    ELSE 
        PRINT '<<< DROPPED FUNCTION fGetFragment >>>'
END
GO
CREATE FUNCTION dbo.fGetFragment
(
    @cmdName varchar(32)  = 'charString'
,   @ID     int          = NULL
,   @Name   varchar(255) = NULL
)
RETURNS varchar(1024)
AS
BEGIN

DECLARE @Frag varchar(1024)

IF @cmdName = 'charString'
BEGIN
    IF @ID IS NULL
    BEGIN
        SELECT
            @Frag = ItemChar
        FROM
            Fragment
        WHERE
            FragmentName = @Name
    END
    ELSE
    BEGIN
        SELECT
            @Frag = ItemChar
        FROM
            Fragment
        WHERE
            FragmentId = @ID
    END
END

IF @cmdName = 'name'
BEGIN
    SELECT
        @Frag = FragmentName
    FROM
        Fragment
    WHERE
        FragmentId = @ID
END

RETURN @Frag
END
GO
GRANT EXECUTE ON fGetFragment TO public
GO
IF OBJECT_ID('fGetFragment') IS NOT NULL 
    PRINT '<<< CREATED FUNCTION fGetFragment >>>' 
ELSE 
    PRINT '<<< FAILED CREATING FUNCTION fGetFragment >>>' 
GO 